use std::{collections::HashMap, sync::Arc};

use tokio::sync::Mutex;

use crate::user::User;

#[derive(Debug)]
pub struct Server {
    pub addr: String,
    pub(crate) users: HashMap<String, Arc<Mutex<User>>>,
}
impl Server {
    pub fn new(addr: String) -> Self {
        Self {
            addr,
            users: HashMap::new(),
        }
    }

    //用户注册
    pub async fn add_user(&mut self, cc_no: String, user: Arc<Mutex<User>>) {
        self.users.insert(cc_no, user.clone());
        self.user_chat_async(user)
    }

    pub fn user_chat_async(&self, user: Arc<Mutex<User>>) {
        tokio::spawn(async move {
            let user_lock = user.lock().await;
            user_lock.handler().await;
        });
    }
    pub(crate) fn remove_user(&mut self, no: String) {
        self.users.remove(&no).unwrap();
    }
}
